<!-- 关联小程序 -->
<template>
	<uni-card spacing="0" :is-shadow="false" :border="false">
		<view class="item-dom">
			<view class="flex-row flex-row-space-between">
				<text class="title1">说明：</text>
			</view>
			<view style="padding:  0 10px;">
				<view>1，开启信息推送才能在微信服务通知里面查看到关于自己的通知</view>
				<view>2，开启信息推送的推送内容包括（咨询通知）</view>
			</view>
		</view>
	</uni-card>
	<button @click="closeOK" v-if="userData.subscribe == 1" class="basicbutton1"
		hover-class="basicbutton1-hover">关闭消息推送</button>
	<button v-else open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" class="basicbutton"
		hover-class="basicbutton-hover">开启消息推送</button>
</template>

<script lang="ts" setup>
	import { setUser, getUser } from "@/utils/storage"
	import { openMsgNotification, closeMsgNotification } from "@/api/user"
	import { ref, onBeforeMount } from 'vue'

	let userData = ref({})
	onBeforeMount(() => {
		userData.value = getUser()
	})
	function decryptPhoneNumber(e) {
		if (e.detail.code) {
			uni.login({ //获取到用户信息
				provider: 'weixin',
				success: (res => {
					let encryptedTel = e.detail.code
					let jsCode = res.code
					messageSwitch(encryptedTel, jsCode)
				}),
				fail: (err) => {
					uni.showToast({
						title: '授权失败取消',
						icon: 'error',
						mask: true,
					});
				}
			})
		}
	}
	function messageSwitch(encryptedTel, jsCode) {
		// #ifdef MP-WEIXIN
		uni.showModal({
			title: '是否授权消息订阅功能',
			success(resdata) {
				if (resdata.confirm) {
					uni.getSetting({
						withSubscriptions: true,
						success: (res) => {
							if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
								if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
									uni.requestSubscribeMessage({
										tmplIds: ['sbtCBlQ8FP-Cjo9Z24U52WSgLBC8AvbTVx6xfog0z9M'],
										success: () => { },
										complete: () => {
											openOk(encryptedTel, jsCode)
										}

									});
								} else {
									uni.requestSubscribeMessage({
										tmplIds: ['sbtCBlQ8FP-Cjo9Z24U52WSgLBC8AvbTVx6xfog0z9M'],
										success: () => { },
										complete: () => {
											openOk(encryptedTel, jsCode)
										}

									});
								}
							} else {
								openOk(encryptedTel, jsCode)
							}
						},
						fail: () => {
							openOk(encryptedTel, jsCode)
						}
					})
				} else if (resdata.cancel) {
					openOk(encryptedTel, jsCode)
				}
			}
		})

		// #endif
		// #ifdef H5 || APP-PLUS 
		openOk(encryptedTel, jsCode)
		// #endif
	}

	// 开启消息推送确定
	function openOk(encryptedTel, jsCode) {
		openMsgNotification({
			encryptedTel,
			jsCode,
			userId: userData.value.userId,
			userType: userData.value.userType
		}).then(res => {
			if (res.code == '200') {
				let data = res.data || {}
				if (data.nickname) {
					uni.showModal({
						title: '提示',
						content: data.nickname,
						showCancel: false,
					});
				}
				userData.value.subscribe = 1
				setUser(userData.value)

			}
		})
	}
	// 关闭消息推送 确定
	function closeOK() {
		closeMsgNotification({
			id: userData.value.id,
		}).then(res => {
			if (res.code == '200') {
				userData.value.subscribe = 0
				setUser(userData.value)
			}
		})
	}
</script>

<style lang="scss" scoped>
	.item-dom {
		font-size: 28rpx;
		color: #999999;
		font-family: "微软雅黑";

		.title1 {
			font-size: 26rpx;
			line-height: 2;
			font-weight: 600;
			color: #01c2b7;
		}
	}

	.basicbutton,
	.basicbutton1 {
		margin: 0 15px;
	}
</style>